//
//  UserMessageViewController.m
//  TeacherNet
//
//  Created by slz on 2017/12/20.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "UserMessageViewController.h"
#import "MessageModel.h"
#import "UserMessageTableViewCell.h"

@interface UserMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) YWTableView *tableView;
@property (nonatomic,strong) MessageModelList *modelList;
@end

@implementation UserMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"我的消息"];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"back_icon" selectImage:nil addTarget:self andAction:@selector(backAction)];
    
    @weakify(self);
    _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self regreshData];
    }];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.estimatedRowHeight = 100;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [_tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

- (void)regreshData
{
    [ZYNetWorking getWithUrl:kAPI_USER_MESSAGE params:@{@"user_id":@"28"} Cache:YES refreshCache:YES success:^(id response) {
        [_tableView.mj_header endRefreshing];
        NSDictionary * dict = [[MFWJson jsonWithObject:response] toDic];
        _modelList = [MessageModelList yy_modelWithDictionary:dict];
        [_tableView reloadData];
    } fail:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        [self.view showTip:error.localizedDescription];
    }];
//    [BaseHttpTool get:kAPI_USER_MESSAGE params:@{@"user_id":@"28"} success:^(id responseObj) {
//
//    } failure:^(NSError *error) {
//        [_tableView.mj_header endRefreshing];
//        [self.view showTip:error.localizedDescription];
//    }];
}



#pragma mark - UITbaleViewDelegate


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel * model = _modelList.messageList[indexPath.row];
    static NSString * reuserIdentifier = @"UserMessageTableViewCell";
    UserMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
    if (!cell) {
        cell = [UserMessageTableViewCell instanceWithNibName:@"UserMessageTableViewCell" owner:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.messageLabel.text = model.msg;
    cell.timeLabel.text = model.time;
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelList.messageList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)changgeAction{
    
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
